package kr.co.nextree.android.sample.activity;

import kr.co.nextree.android.sample.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SampleOptionMenuCode extends Activity {
	private static final int MENU_ONE = 1;
	private static final int MENU_TWO = 2;
	private static final int MENU_THREE = 3;
	private static final int MENU_FOUR = 4;
	private static final int MENU_FIVE = 5;
	private static final int MENU_SIX = 6;
	private static final int MENU_SEVEN = 7;
	
	private Button mBtnOpen;
	private Button mBtnClose;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.option_menu_layout);
		
		mBtnOpen = (Button)findViewById(R.id.open);
		mBtnOpen.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				openOptionsMenu();
			}
		});
		
		mBtnClose = (Button)findViewById(R.id.close);
		mBtnClose.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				closeOptionsMenu();
			}
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		SubMenu sm = menu.addSubMenu(0, MENU_ONE, Menu.NONE, "메뉴1").setIcon(android.R.drawable.ic_menu_add);
		sm.add(1, 0, Menu.NONE, "SubMenu1");
		sm.add(0, 1, Menu.NONE, "SubMenu2");
		
		menu.add(0, MENU_TWO, Menu.NONE, "메뉴2");
		menu.add(1, MENU_THREE, Menu.NONE, "메뉴3");
		menu.add(1, MENU_FOUR, Menu.NONE, "메뉴4");
		menu.add(0, MENU_FIVE, Menu.NONE, "메뉴5");
		menu.add(0, MENU_SIX, Menu.NONE, "메뉴6");
		menu.add(0, MENU_SEVEN, Menu.NONE, "메뉴7");

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ONE:
		case MENU_TWO:
		case MENU_THREE:
		case MENU_FOUR:
		case MENU_FIVE:
		case MENU_SIX:
		case MENU_SEVEN:
			Toast.makeText(this, item.getItemId() + "메뉴선택 ", Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		//menu.setGroupEnabled(1, false);
		//menu.getItem(0).getSubMenu().setGroupEnabled(1, false);
		
		return super.onPrepareOptionsMenu(menu);
	}

}
